Sys.setenv(JAVA_HOME='/usr/local/software/spack/spack-0.11.2/opt/spack/linux-rhel7-x86_64/gcc-5.4.0/jdk-8u141-b15-p4aaoptkqukgdix6dh5ey236kllhluvr/jre') #Ubuntu cluster
#Sys.setenv(JAVA_HOME= "/usr/lib/jvm/java-11-openjdk-amd64")

.libPaths("/home/hs621/renv/lib/R/library")

## Load packages
library(nlrx)
library(dplyr)
library(tidyr)
#library(rcartocolor)
#library(ggthemes) 
library(feather)

# Office
netlogopath <- file.path("/usr/local/Cluster-Apps/netlogo/6.0.4")
outpath <- file.path("/home/hs621/github/chapter5")



## Step1: Create a nl obejct:
nl <- nl(nlversion = "6.0.4",
         nlpath = netlogopath,
         modelpath = file.path(outpath, "CBD_Cars_July01.nlogo"),
         jvmmem = 1024)

checkpoint <- c(1, 99901, 99931, 99961, 99991, 100021, 100051, 100081, 100111, 100141, 100171) %>% as.integer()
# "Jan 17th", "Jan 20th", "Feb 10th", "Feb 23rd", "Feb 28th", "Mar 12th", "Mar 24th", "Mar 25th"


## Step2: Add Experiment
nl@experiment <- experiment(expname = "nlrx_spatial",
                            outpath = outpath,
                            repetition = 1,      
                            tickmetrics = "true",
                            idsetup = "setup",   
                            idgo = "go",
                            runtime = 127740,
                            evalticks= checkpoint,
                            metrics.turtles = list("employees" = "health",
                                                   "drivers" = "health"),
                            variables = list("awareness" = list(values = c("\"yes\"","\"no\"" ))),
                            constants = list("health_loss" = 0.01,
                                             'emission-factor' = 5,
                                             'car_ratio' = .03,
                                             "medication" = 1)  
)

# Evaluate if variables and constants are valid:
eval_variables_constants(nl)

#nl@simdesign <- simdesign_ff(nl = nl, nseeds = 1)
#nl@simdesign <- simdesign_simple(nl = nl, nseeds = 1)
nl@simdesign <- simdesign_distinct(nl=nl, nseeds=1)


# Step4: Run simulations:
init <- Sys.time()
results <- run_nl_all(nl = nl)
Sys.time() - init

gc()

results$`random-seed`[1]

#
save(results, file = paste("HC_", results$`random-seed`[1], ".RData", sep = ""))

